<template>
  <div>
    <div class="bar">
      <el-button type="primary" @click="create">新增</el-button>
    </div>
    <el-table :data="rights">
      <el-table-column prop="name" label="名称" width="180"></el-table-column>
      <el-table-column prop="code" label="代码"></el-table-column>
      <el-table-column label="操作" width="200" inline-template align="center">
        <template>
          <el-button @click="modify(row)" type="text" size="small">修改</el-button>
          <!--<el-button @click="del(row.pkid)" type="text" size="small">删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="按钮权限" v-model="dialogFormVisible">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="名称" required prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="代码" required prop="code">
          <el-input v-model="form.code" :disabled="!!form.pkid" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>
  .bar{
    padding: 10px;
    box-shadow: 0 0px 8px rgba(0, 0, 0, 0.25);
    position: relative;
  }
</style>
<script>
  import rightApi from '../../../api/sys/rightApi'
  export default{
    data () {
      return {
        dialogFormVisible: false,
        rights: [],
        form: {pkid: null, name: '', code: ''},
        rules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          right: [
            { required: true, message: '请输入代码', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      rightApi.all().then(resp => {
        this.rights = resp.body.data || []
      })
    },
    methods: {
      save () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            rightApi.save(this.form).then((resp) => {
              if (!this.form.pkid) {
                this.form.pkid = resp.body.data.pkid
                this.rights.push(this.form)
              }
              this.dialogFormVisible = false
              this.$message({
                showClose: true,
                message: '保存成功',
                type: 'success'
              })
            })
          } else {
            return false
          }
        })
      },
      create () {
        this.dialogFormVisible = true
        this.form = {pkid: null, name: '', code: ''}
      },
      modify (right) {
        this.dialogFormVisible = true
        this.form = right
      },
      del (pkid) {
        this.$confirm('确认删除吗?', '提示', {
          type: 'warning'
        }).then(() => {
          rightApi.delete(pkid).then(resp => {
            this.$message({
              showClose: true,
              message: '删除成功',
              type: 'success'
            })
            this.rights = this.rights.filter(right => right.pkid !== pkid)
          })
        },
        () => {})
      }
    }
  }
</script>
